package com.xiaotian.common.exception;


import com.xiaotian.common.code.CommonCode;
import com.xiaotian.common.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionCatch {

    @ExceptionHandler(CustomException.class)
    public Result customException(CustomException e) {
        log.error("catch CustomException:{} " + e.getMessage());
        e.printStackTrace();
        return new Result(e.getResultCode());
    }

    @ExceptionHandler(Exception.class)
    public Result exception(Exception e) {
        log.error("catch Exception:{} " + e.getMessage());
        e.printStackTrace();
        // 返回错误
        Result result = new Result(CommonCode.SERVER_ERROR);
        return result;
    }
}
